Array.prototype.indexOf = function(val) {
	for ( var i = 0; i < this.length; i++) {
		if (this[i] == val)
			return i;
	}
	return -1;
};
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
var selectedId = new Array();
var isRightCheck = false;
var winWidth = 0;
var numNum = 0;

function divLoad() {
	// winWidth = $(window).width();
	// numNum = parseInt(winWidth / 1600 * 9);
	// $(".subbus").each(function() {
	// var str = $.trim($(this).html());
	// $(this).html(sub(str, numNum));
	// });
	var gcId = $('#selectedGcString').val();
	// 循环已经选择的广场id默认选中
	var gcList = gcId.split(",");
	for ( var i = 0; i < gcList.length; i++) {
		selectedId.push(gcList[i]);
		// 修改样式使选中
		$('#' + gcList[i]).removeClass("checkbox");
		$('#' + gcList[i]).addClass("checkboxed");
		var content = "<div id='selectdiv"
				+ gcList[i]
				+ "' class='floatleft szqy'><div class='floatleft checkbox' id='childdiv"
				+ gcList[i]
				+ "'><div class='floatleft selectedjuzhong'>"
				+ $('#' + gcList[i])[0].children[0].innerText
				+ "</div><div onclick='deleteYxz(&quot;"
				+ gcList[i]
				+ "&quot;)' class='floatleft selecteddel heigthall'></div></div></div>";
		$('#yixuanzeDiv').append(content);
	}
	$("#guojiaCombo>option").remove();
	$('#guojiaCombo').html("<option value='-1'>请选择</option>");
	var guojiaList = eval($('#guojia').val());
	for ( var i = 0; i < guojiaList.length; i++) {
		$(
				"<option value=" + guojiaList[i].siteid + ">"
						+ guojiaList[i].sitename + "</option>").appendTo(
				$('#guojiaCombo'));
	}
}
/**
 * 点击地区选择广场
 * 
 * @param id
 * @return
 */
function diquClick(id) {
	$('#gc_dqSiteId').attr("value", id);
	ajaxSearchGc(1);
}

/**
 * 点击商圈选择广场
 * 
 * @param id
 * @return
 */
function shangquanClick(id) {
	$('#gc_sqSiteId').attr("value", id);
	ajaxSearchGc(2);
}

// 广场-区划点击不限
function quhuaClick(id) {
	if (id == -1) {
		$("#guojiaCombo>option").remove();
		$("#shengCombo>option").remove();
		$("#shiCombo>option").remove();
		$("#quCombo>option").remove();
		$('#guojiaCombo').html("<option value='-1'>请选择</option>");
		$('#shengCombo').html("<option value='-1'>请选择</option>");
		$('#shiCombo').html("<option value='-1'>请选择</option>");
		$('#quCombo').html("<option value='-1'>请选择</option>");
		$('#gc_countryId').attr("value", "");
		$('#gc_provinceId').attr("value", "");
		$('#gc_cityId').attr("value", "");
		$('#gc_areaId').attr("value", "");
		var guojiaList = eval($('#guojia').val());
		for ( var i = 0; i < guojiaList.length; i++) {
			$(
					"<option value=" + guojiaList[i].siteid + ">"
							+ guojiaList[i].sitename + "</option>").appendTo(
					$('#guojiaCombo'));
		}
		ajaxSearchGc(3);
	}
}

// 广场-选择区划
function comboChange(id) {
	isRightCheck = false;
	var guojia = $('#guojiaCombo').val();
	var sheng = $('#shengCombo').val();
	var shi = $('#shiCombo').val();
	var qu = $('#quCombo').val();
	var changeId = "-1";
	// 如果选择了国家
	if (id == 0) {
		$("#shengCombo>option").remove();
		$("#shiCombo>option").remove();
		$("#quCombo>option").remove();
		$('#shengCombo').html("<option value='-1'>请选择</option>");
		$('#shiCombo').html("<option value='-1'>请选择</option>");
		$('#quCombo').html("<option value='-1'>请选择</option>");
		changeId = guojia;
		$('#gc_countryId').attr("value", guojia);
		$('#gc_provinceId').attr("value", "");
		$('#gc_cityId').attr("value", "");
		$('#gc_areaId').attr("value", "");
	}
	// 如果选择了省
	else if (id == 1) {
		$("#shiCombo>option").remove();
		$("#quCombo>option").remove();
		$('#shiCombo').html("<option value='-1'>请选择</option>");
		$('#quCombo').html("<option value='-1'>请选择</option>");
		changeId = sheng;
		$('#gc_provinceId').attr("value", sheng);
		$('#gc_cityId').attr("value", "");
		$('#gc_areaId').attr("value", "");
	}
	// 如果选择了市
	else if (id == 2) {
		$("#quCombo>option").remove();
		$('#quCombo').html("<option value='-1'>请选择</option>");
		changeId = shi;
		$('#gc_cityId').attr("value", shi);
		$('#gc_areaId').attr("value", "");
	}
	// 如果选择了区
	else if (id == 3) {
		changeId = qu;
		$('#gc_areaId').attr("value", qu);
	}
	// 初始化广场并框中
	else if (id == 4) {
		isRightCheck = true;
		$("#shengCombo>option").remove();
		$("#shiCombo>option").remove();
		$("#quCombo>option").remove();
		$('#shengCombo').html("<option value='-1'>请选择</option>");
		$('#shiCombo').html("<option value='-1'>请选择</option>");
		$('#quCombo').html("<option value='-1'>请选择</option>");
		changeId = "-1";
		$('#guojiaCombo').attr("value", "-1");
		$('#gc_countryId').attr("value", "-1");
		$('#gc_provinceId').attr("value", "");
		$('#gc_cityId').attr("value", "");
		$('#gc_areaId').attr("value", "");
	}
	// 下拉框联动
	$.ajax( {
		type : "GET",
		contentType : "application/json",
		data : {
			type : id,
			id : changeId
		},
		dataType : 'json',
		url : "searchCombo",
		async : true,
		success : function(msgs) {
			var siteList = msgs.siteList;
			if (siteList != null && siteList.length > 0) {
				if (id == 0) {
					for ( var i = 0; i < siteList.length; i++) {
						$(
								"<option value=" + siteList[i].siteid + ">"
										+ siteList[i].sitename + "</option>")
								.appendTo($('#shengCombo'));
					}
				} else if (id == 1) {
					for ( var i = 0; i < siteList.length; i++) {
						$(
								"<option value=" + siteList[i].siteid + ">"
										+ siteList[i].sitename + "</option>")
								.appendTo($('#shiCombo'));
					}
				} else if (id == 2) {
					for ( var i = 0; i < siteList.length; i++) {
						$(
								"<option value=" + siteList[i].siteid + ">"
										+ siteList[i].sitename + "</option>")
								.appendTo($('#quCombo'));
					}
				}
			}
		},
		error : function(mm, a, b, c, d, e) {
		}
	});
	ajaxSearchGc(3);
}

// 删除搜索栏中的内容
function clearssk() {
	$("#gc_gcSiteName").val("");
	$("#imgx").hide();
	ajaxSearchGc(1);
}

// 搜索栏查询广场
function searchGc() {
	// 广场名称
	var gc = $('#gc_gcSiteName').val().trim();
	if (gc == "" || gc == null) {
		$('#gc_gcInput').attr("value", "");
	} else {
		$('#gc_gcInput').attr("value", gc);
	}
	ajaxSearchGc(0);
}

/**
 * ajax请求查询广场数据
 * 
 * @param id
 * @return
 */
function ajaxSearchGc(id) {
	// 地区
	var diqu = $('#gc_dqSiteId').val();
	// 国家
	var guojia = $('#gc_countryId').val();
	// 省
	var sheng = $('#gc_provinceId').val();
	// 市
	var shi = $('#gc_cityId').val();
	// 区
	var qu = $('#gc_areaId').val();
	// 商圈
	var shangquan = $('#gc_sqSiteId').val();
	// 搜索栏中广场名称
	var guangchang = $('#gc_gcInput').val();
	if (guangchang != undefined && guangchang != "" && guangchang != null) {
		guangchang = guangchang.trim();
	}
	// 如果是点击搜索栏查询广场
	if (id == 0) {
		// diqu = "";
		// guojia = "";
		// sheng = "";
		// shi = "";
		// qu = "";
		// shangquan = "";
	}
	// 如果是选中条件查询广场
	else if (id == 1 || id == 2 || id == 3) {
		// 清空搜索栏
		$('#gc_gcSiteName').attr("value", "");
		// $('#imgx').hide();
		guangchang = "";
	}
	$
			.ajax( {
				type : "GET",
				contentType : "application/json",
				data : {
					diqu : diqu,
					guojia : guojia,
					sheng : sheng,
					shi : shi,
					qu : qu,
					shangquan : shangquan,
					guangchang : encodeURI(guangchang)
				},
				dataType : 'json',
				url : "searchGcSiteSingle",
				async : true,
				success : function(msgs) {
					// 获取结果
				var pageResult = msgs.gcList;
				$('#gcResultDiv').empty();
				if (pageResult.length == 0) {
					$('#gcResultDiv').append("没有您要查询的数据！");
				} else {
					winWidth = $(window).width();
					numNum = parseInt(winWidth / 1600 * 15);
					for ( var i = 0; i < pageResult.length; i++) {
						var content = "<div class='em45'><div class='em35'><div onclick='leftGcCheck(&quot;"
								+ pageResult[i].siteid
								+ "&quot;)' id='"
								+ pageResult[i].siteid
								+ "' class='floatleft divcheck'><div class='divchecked hide'></div></div><span class='floatleft kongxi'></span><div title='"
								+ pageResult[i].sitename
								+ "' class='em35 lineh35 floatleft' style='width: 70%'>"
								+ sub(pageResult[i].sitename, numNum)
								+ "</div></div></div>";
						$('#gcResultDiv').append(content);
					}
					for ( var i = 0; i < selectedId.length; i++) {
						// 修改样式使选中
						$('#' + selectedId[i]).children().removeClass("hide");
					}
				}
			},
			error : function(mm, a, b, c, d, e) {
			}
			});
}

// 清空
function qingkong() {
	// myConfirm("提示", "您是否要清空已选择？", qingkongFun);
	if (confirm("您是否要清空已选择？")) {
		qingkongFun();
	}
}

function qingkongFun() {
	clearYxz();
	// 初始化广场结果
	$('#gc_dqSiteId').attr("value", "-1");
	$("#diqu a").removeClass("clickDiqu");
	$('#dq_buxian').addClass("clickDiqu");
	diquClick("-1");
	comboChange(4);
}

// 清空已选择
function clearYxz() {
	// 清空左侧已选择
	for ( var i = 0; i < selectedId.length; i++) {
		$('#' + selectedId[i]).children().addClass("hide");
		$('#selected' + selectedId[i]).children().addClass("hide");
	}
	// 清空已选择数组
	selectedId.length = 0;
}

// 左侧广场点击
function leftGcCheck(id) {
	// 获取选择广场类型-单选、多选
	var selectType = $('#selectType').val();
	// 如果是选中事件
	if ($('#' + id).children().hasClass("hide")) {
		// 如果是单选
		if (selectType == 0) {
			clearYxz();
		}
		selectedId.push(id);
		$('#' + id).children().removeClass("hide");
		$('#selected' + id).children().removeClass("hide");
	}
	// 如果是取消选中事件
	else {
		// 删除已选中数组中的id
		for ( var i = 0; i < selectedId.length; i++) {
			if (selectedId[i] == id) {
				selectedId.remove(selectedId[i]);
				break;
			}
		}
		$('#' + id).children().addClass("hide");
		$('#selected' + id).children().addClass("hide");
	}
}

// 右侧广场点击
function rightGcCheck(id) {
	// 获取选择广场类型-单选、多选
	var selectType = $('#selectType').val();
	var newId = id.substring(8);
	// 如果是选中事件
	if ($('#' + id).children().hasClass("hide")) {
		// 如果是单选
		if (selectType == 0) {
			clearYxz();
		}
		selectedId.push(newId);
		$('#' + newId).children().removeClass("hide");
		$('#selected' + newId).children().removeClass("hide");
	}
	// 如果是取消选中事件
	else {
		// 删除已选中数组中的id
		for ( var i = 0; i < selectedId.length; i++) {
			if (selectedId[i] == newId) {
				selectedId.remove(selectedId[i]);
				break;
			}
		}
		$('#' + newId).children().addClass("hide");
		$('#selected' + newId).children().addClass("hide");
	}
}

// 删除已选择中的div
function deleteYxz(id) {
	// 修改样式置为不选中
	$('#' + id).removeClass("checkboxed");
	$('#' + id).addClass("checkbox");
	// 删除已选中数组中的id
	for ( var i = 0; i < selectedId.length; i++) {
		if (selectedId[i] == id) {
			selectedId.remove(selectedId[i]);
			break;
		}
	}
	// 从已选择中删除div
	$('#selectdiv' + id).remove();
}

// 确定
function queding() {
	if (selectedId.length < 1) {
		alert("请至少选择一个场所！");
		return;
	}
	showLoading();
	$('#changePlaza').hide();
	var myForm = $('#backForm');
	var siteId = "";
	for ( var i = 0; i < selectedId.length; i++) {
		siteId += selectedId[i] + ",";
	}
	siteId = siteId.substring(0, siteId.length - 1);
	$('#backGcSiteString').attr("value", siteId);
	myForm.submit();
}

// 取消
function quxiao() {
	if (confirm("您是否要取消已选择的场所？")) {
		var gcId = $('#selectedGcString').val();
		var myForm = $('#backForm');
		$('#backGcSiteString').attr("value", gcId);
		myForm.submit();
	}
}
// 筛选按钮
function shaixuan() {
	var imgsrc = $("#sxkimg").attr("src");
	if (imgsrc.indexOf("sx01.png") > 0) {
		$("#sxkimg").attr("src", "images/sx02.png");
		$("#sxdiv").hide();
	} else {
		$("#sxkimg").attr("src", "images/sx01.png");
		$("#sxdiv").show();
	}
}
function showx() {
	var txtssk = $("#gc_gcSiteName").val();
	if (txtssk == "") {
		$("#imgx").hide();
	} else {
		$("#imgx").show();
	}
}
$(function() {
	$(".saixuanqu1 a").click(function() {
		$(".saixuanqu1 a").removeClass("sxxiangaselect");
		$(this).addClass("sxxiangaselect");
	});
	$(".saixuanqu2 a").click(function() {
		$(".saixuanqu2 a").removeClass("sxxiangaselect");
		$(this).addClass("sxxiangaselect");
	});
	$(".saixuanqu3 a").click(function() {
		$(".saixuanqu3 a").removeClass("sxxiangaselect");
		$(this).addClass("sxxiangaselect");
	});
});